<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Enums\CheckStatusEnum;
use App\Format\PassCardFormat;
use App\Http\Resources\PassCardForAdmin;
use App\Services\PassCardService;
use App\Traits\ExcelTrait;
use Illuminate\Http\Request;

class PassCardController extends Controller
{
    use ExcelTrait;

    protected $passCardService;

    public function __construct(PassCardService $passCardService)
    {
        $this->passCardService = $passCardService;
    }

    public function getList(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];

        if ($request->input('check_status')) {
            $search['check_status'] = $request->input('check_status');
        }
        if ($request->input('name')) {
            $search['name'] = $request->input('name');
        }
        if ($request->input('now_address')) {
            $search['now_address'] = $request->input('now_address');
        }
        if ($request->input('area')) {
            $search['area'] = $request->input('area');
        }
        $res = $this->passCardService->getList($page, $pageSize, $search);

        return renderSuccessJson($res);
    }

    public function detail(Request $request)
    {
        $id = $request->input('id');
        $res = $this->passCardService->detail($id);
        return renderSuccessJson($res);
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        $this->passCardService->delete($id);
        return renderSuccessJson();
    }

    public function create(Request $request)
    {
        $id_card_number = $request->input('id_card_number');
        $identity = $request->input('identity');
        $format = new PassCardFormat($request->all());
        $this->passCardService->createPassCardFromAdmin($format, $id_card_number, $identity, CheckStatusEnum::CHECK_ING);
        return renderSuccessJson();
    }

    public function export(Request $request)
    {
        $search = [];
        if ($request->input('name')) {
            $search['name'] = $request->input('name');
        }
        if ($request->input('check_status')) {
            $search['check_status'] = $request->input('check_status');
        }
        if ($request->input('now_address')) {
            $search['now_address'] = $request->input('now_address');
        }
        if ($request->input('area')) {
            $search['area'] = $request->input('area');
        }
        if ($request->input('ids')) {
            $search['ids'] = explode(',', $request->input('ids'));
        }

        $header = [
            'name' => '姓名',
            'phone' => '联系方式',
            'now_address' => '现居住地址',
            'flow_register_code' => '流管登记编码',
            'pass_card_photo' => '出入证照片',
        ];

        $passService = new PassCardService();
        $list = $passService->getExportData($search);
        $res = PassCardForAdmin::collection($list);
        return $this->excelExport($header, $res->resolve(), '出入证信息');
    }

    public function saveDraft(Request $request)
    {
        $format = new PassCardFormat($request->all());
        $id_card_number = $request->input('id_card_number');
        $identity = $request->input('identity');
        $this->passCardService->createPassCardFromAdmin($format, $id_card_number, $identity, 4);
        return renderSuccessJson();
    }
}
